package com.railway.bean;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Train {
	private int train_id;//列车id
	
	private String train_name;//列车名
	
	private int train_type;//列车类型
	
	private int starting_station_id;//起点站id
	
	private String start_station;//起点站
	
	private int terminal_station_id;//终点站id
	
	private String terminal_station;//终点站
	
	private Date starting_time;//出发时间
	
	private Date terminal_time;//终点时间
	
	private int train_size;//列车长度
	
	private List<Pass> pass;//途径车站
	
	private List<Carriage> carriage;//车厢
	
	private int start_pass_id ;//起点途径id
	
	private int terminal_pass_id;//终点途径id
	
	private List<Ticket> ticket;//票
	
	private double count_distance = 0;//总距离
	
	private int unsold_vip_seat_num;//贵宾座剩余数量
	
	private int unsold_first_seat_num;//一等座剩余数量
	
	private int unsold_second_seat_num;//二等座剩余数量
	
	private double vip_seatPrivce ;//vip座价格
	
	private double first_seatPrivce;//一等座价格
	
	private double second_seatPrivce;//二等座价格
	
	private int is_exist;//是否停运
	
	public double getVip_seatPrivce() {
		return count_distance*1.4+5;
	}

	public double getFirst_seatPrivce() {
		return count_distance*1.1+5;
	}

	public double getSecond_seatPrivce() {
		return count_distance*0.8+5;
	}

	public int getUnsold_vip_seat_num() {
		return selectUnsoldSeat(1);
	}

	public int getUnsold_first_seat_num() {
		return selectUnsoldSeat(2);
	}

	public int getUnsold_second_seat_num() {
		return selectUnsoldSeat(3);
	}

	
	public int getIs_exist() {
		return is_exist;
	}

	public void setIs_exist(int is_exist) {
		this.is_exist = is_exist;
	}

	public void setCount_distance(double count_distance) {
		this.count_distance = count_distance;
	}

	public void setUnsold_vip_seat_num(int unsold_vip_seat_num) {
		this.unsold_vip_seat_num = unsold_vip_seat_num;
	}

	public void setUnsold_first_seat_num(int unsold_first_seat_num) {
		this.unsold_first_seat_num = unsold_first_seat_num;
	}

	public void setUnsold_second_seat_num(int unsold_second_seat_num) {
		this.unsold_second_seat_num = unsold_second_seat_num;
	}

	
	/**计算余票
	 * @param seat_type
	 * @return
	 */
	public int selectUnsoldSeat(int seat_type) {
		
		int seat_num = 0;
		if(carriage!= null) {
			for (Carriage c : carriage) {
				if(c.getCarriage_type() == seat_type) {
					
					seat_num = seat_num +(5*c.getSeat_num());//计算出所有此类型的座位
				}
			}
		}
		
		//将已经被购买的座位去除
		if(ticket != null) {
			for (Ticket t : ticket) {
				if(t.getStart_pass_id()<start_pass_id && t.getTerminal_pass_id()>start_pass_id && t.getSeatType() == seat_type) {
					seat_num--;
				}else if(t.getStart_pass_id()<terminal_pass_id && t.getTerminal_pass_id()>terminal_pass_id && t.getSeatType() == seat_type){
					seat_num--;
				}
			}
		}
		return seat_num;
	}
	
	public int getStart_pass_id() {
		return start_pass_id;
	}

	public void setStart_pass_id(int start_pass_id) {
		this.start_pass_id = start_pass_id;
	}

	public int getTerminal_pass_id() {
		return terminal_pass_id;
	}

	public void setTerminal_pass_id(int terminal_pass_id) {
		this.terminal_pass_id = terminal_pass_id;
	}

	public String getTerminal_station() {
		return terminal_station;
	}

	public void setTerminal_station(String terminal_station) {
		this.terminal_station = terminal_station;
	}

	public void setStarting_time() {
		for (Pass p : pass) {
			if(p.getStation_id()==starting_station_id) {
				starting_time = p.getPass_time();
			}
		}
		
	}

	public void setTerminal_time(Date terminal_time) {
		this.terminal_time = terminal_time;
	}

	public int getTrain_id() {
		return train_id;
	}

	public void setTrain_id(int train_id) {
		this.train_id = train_id;
	}

	public String getTrain_name() {
		return train_name;
	}

	public void setTrain_name(String train_name) {
		this.train_name = train_name;
	}

	public int getTrain_type() {
		return train_type;
	}

	public void setTrain_type(int train_type) {
		this.train_type = train_type;
	}

	public int getStarting_station_id() {
		return starting_station_id;
	}

	public void setStarting_station_id(int starting_station_id) {
		this.starting_station_id = starting_station_id;
	}

	public String getStart_station() {
		return start_station;
	}

	public void setStart_station(String start_station) {
		this.start_station = start_station;
	}

	public int getTerminal_station_id() {
		return terminal_station_id;
	}

	public void setTerminal_station_id(int terminal_station_id) {
		this.terminal_station_id = terminal_station_id;
	}


	public int getTrain_size() {
		return train_size;
	}

	public void setTrain_size(int train_size) {
		this.train_size = train_size;
	}

	public List<Pass> getPass() {
		return pass;
	}

	public void setPass(List<Pass> pass) {
		this.pass = pass;
		
		for (Pass p : pass) {
			if(p.getStation_id()==starting_station_id) {
				starting_time = p.getPass_time();
			}
		}
		
		for (Pass p : pass) {
			if(p.getStation_id()==terminal_station_id) {
				terminal_time = p.getPass_time();
			}
		}
	}

	public List<Carriage> getCamage() {
		return carriage;
	}

	public void setCamage(List<Carriage> carriage) {
		this.carriage = carriage;
		train_size = carriage.size();
	}

	public double getCount_distance() {
		if(pass != null) {
			for (Pass p : pass) {
				if(p.getPass_id() >= start_pass_id && p.getPass_id() <= terminal_pass_id) {
					count_distance += p.getNext_distance();
				}
			}
		}
		return count_distance;
	}

	public Train() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public Date getStarting_time() {
		return starting_time;
	}

	public Date getTerminal_time() {
		return terminal_time;
	}

	public Train(int train_id, String train_name, int train_type, int starting_station_id, String start_station,
			int terminal_station_id, Date starting_time, Date terminal_time, int train_size, List<Pass> pass,
			List<Carriage> carriage, double count_distance) {
		super();
		this.train_id = train_id;
		this.train_name = train_name;
		this.train_type = train_type;
		this.starting_station_id = starting_station_id;
		this.start_station = start_station;
		this.terminal_station_id = terminal_station_id;
		this.starting_time = starting_time;
		this.terminal_time = terminal_time;
		this.train_size = train_size;
		this.pass = pass;
		this.carriage = carriage;
		this.count_distance = count_distance;
	}

	
	public Train(int train_id, String train_name, int train_type, int starting_station_id, int terminal_station_id,
			Date starting_time, Date terminal_time, double count_distance) {
		super();
		this.train_id = train_id;
		this.train_name = train_name;
		this.train_type = train_type;
		this.starting_station_id = starting_station_id;
		this.terminal_station_id = terminal_station_id;
		this.starting_time = starting_time;
		this.terminal_time = terminal_time;
		this.count_distance = count_distance;
	}

	@Override
	public String toString() {
		return "Train [train_id=" + train_id + ", train_name=" + train_name + ", train_type=" + train_type
				+ ", starting_station_id=" + starting_station_id + ", start_station=" + start_station
				+ ", terminal_station_id=" + terminal_station_id + ", terminal_station=" + terminal_station
				+ ", starting_time=" + starting_time + ", terminal_time=" + terminal_time + ", train_size=" + train_size
				+ ", pass=" + pass + ", carriage=" + carriage + ", start_pass_id=" + start_pass_id + ", terminal_pass_id="
				+ terminal_pass_id + ", count_distance=" + count_distance + ", unsold_vip_seat_num="
				+ unsold_vip_seat_num + ", unsold_first_seat_num=" + unsold_first_seat_num + ", unsold_second_seat_num="
				+ unsold_second_seat_num + "]";
	}
}
